using System.Globalization;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Media;

namespace GraphMyCode.Client.Utils
{
	public static class ColorConverter
	{

		private static readonly List<KeyValuePair<string, string>> Lookup;


		static ColorConverter()
		{
			Lookup = new List<KeyValuePair<string, string>>
			         	{
			         		new KeyValuePair<string, string>("#fff0f8ff", "AliceBlue"),
			         		new KeyValuePair<string, string>("#fffaebd7", "AntiqueWhite"),
			         		new KeyValuePair<string, string>("#ff7fffd4", "Aquamarine"),
			         		new KeyValuePair<string, string>("#fff0ffff", "Azure"),
			         		new KeyValuePair<string, string>("#fff5f5dc", "Beige"),
			         		new KeyValuePair<string, string>("#ffffe4c4", "Bisque"),
			         		new KeyValuePair<string, string>("#ff000000", "Black"),
			         		new KeyValuePair<string, string>("#ffffebcd", "BlanchedAlmond"),
			         		new KeyValuePair<string, string>("#ff0000ff", "Blue"),
			         		new KeyValuePair<string, string>("#ff8a2be2", "BlueViolet"),
			         		new KeyValuePair<string, string>("#ffa52a2a", "Brown"),
			         		new KeyValuePair<string, string>("#ffdeb887", "BurlyWood"),
			         		new KeyValuePair<string, string>("#ff5f9ea0", "CadetBlue"),
			         		new KeyValuePair<string, string>("#ff7fff00", "Chartreuse"),
			         		new KeyValuePair<string, string>("#ffd2691e", "Chocolate"),
			         		new KeyValuePair<string, string>("#ffff7f50", "Coral"),
			         		new KeyValuePair<string, string>("#ff6495ed", "CornflowerBlue"),
			         		new KeyValuePair<string, string>("#fffff8dc", "Cornsilk"),
			         		new KeyValuePair<string, string>("#ffdc143c", "Crimson"),
			         		new KeyValuePair<string, string>("#ff00ffff", "Cyan"),
			         		new KeyValuePair<string, string>("#ff00008b", "DarkBlue"),
			         		new KeyValuePair<string, string>("#ff008b8b", "DarkCyan"),
			         		new KeyValuePair<string, string>("#ffb8860b", "DarkGoldenrod"),
			         		new KeyValuePair<string, string>("#ffa9a9a9", "DarkGray"),
			         		new KeyValuePair<string, string>("#ff006400", "DarkGreen"),
			         		new KeyValuePair<string, string>("#ffbdb76b", "DarkKhaki"),
			         		new KeyValuePair<string, string>("#ff8b008b", "DarkMagenta"),
			         		new KeyValuePair<string, string>("#ff556b2f", "DarkOliveGreen"),
			         		new KeyValuePair<string, string>("#ffff8c00", "DarkOrange"),
			         		new KeyValuePair<string, string>("#ff9932cc", "DarkOrchid"),
			         		new KeyValuePair<string, string>("#ff8b0000", "DarkRed"),
			         		new KeyValuePair<string, string>("#ffe9967a", "DarkSalmon"),
			         		new KeyValuePair<string, string>("#ff8fbc8f", "DarkSeaGreen"),
			         		new KeyValuePair<string, string>("#ff483d8b", "DarkSlateBlue"),
			         		new KeyValuePair<string, string>("#ff2f4f4f", "DarkSlateGray"),
			         		new KeyValuePair<string, string>("#ff00ced1", "DarkTurquoise"),
			         		new KeyValuePair<string, string>("#ff9400d3", "DarkViolet"),
			         		new KeyValuePair<string, string>("#ffff1493", "DeepPink"),
			         		new KeyValuePair<string, string>("#ff00bfff", "DeepSkyBlue"),
			         		new KeyValuePair<string, string>("#ff696969", "DimGray"),
			         		new KeyValuePair<string, string>("#ff1e90ff", "DodgerBlue"),
			         		new KeyValuePair<string, string>("#ffb22222", "Firebrick"),
			         		new KeyValuePair<string, string>("#fffffaf0", "FloralWhite"),
			         		new KeyValuePair<string, string>("#ff228b22", "ForestGreen"),
			         		new KeyValuePair<string, string>("#ffdcdcdc", "Gainsboro"),
			         		new KeyValuePair<string, string>("#fff8f8ff", "GhostWhite"),
			         		new KeyValuePair<string, string>("#ffffd700", "Gold"),
			         		new KeyValuePair<string, string>("#ffdaa520", "Goldenrod"),
			         		new KeyValuePair<string, string>("#ff808080", "Gray"),
			         		new KeyValuePair<string, string>("#ff008000", "Green"),
			         		new KeyValuePair<string, string>("#ffadff2f", "GreenYellow"),
			         		new KeyValuePair<string, string>("#fff0fff0", "Honeydew"),
			         		new KeyValuePair<string, string>("#ffff69b4", "HotPink"),
			         		new KeyValuePair<string, string>("#ffcd5c5c", "IndianRed"),
			         		new KeyValuePair<string, string>("#ff4b0082", "Indigo"),
			         		new KeyValuePair<string, string>("#fffffff0", "Ivory"),
			         		new KeyValuePair<string, string>("#fff0e68c", "Khaki"),
			         		new KeyValuePair<string, string>("#ffe6e6fa", "Lavender"),
			         		new KeyValuePair<string, string>("#fffff0f5", "LavenderBlush"),
			         		new KeyValuePair<string, string>("#ff7cfc00", "LawnGreen"),
			         		new KeyValuePair<string, string>("#fffffacd", "LemonChiffon"),
			         		new KeyValuePair<string, string>("#ffadd8e6", "LightBlue"),
			         		new KeyValuePair<string, string>("#fff08080", "LightCoral"),
			         		new KeyValuePair<string, string>("#ffe0ffff", "LightCyan"),
			         		new KeyValuePair<string, string>("#fffafad2", "LightGoldenrodYellow"),
			         		new KeyValuePair<string, string>("#ffd3d3d3", "LightGray"),
			         		new KeyValuePair<string, string>("#ff90ee90", "LightGreen"),
			         		new KeyValuePair<string, string>("#ffffb6c1", "LightPink"),
			         		new KeyValuePair<string, string>("#ffffa07a", "LightSalmon"),
			         		new KeyValuePair<string, string>("#ff20b2aa", "LightSeaGreen"),
			         		new KeyValuePair<string, string>("#ff87cefa", "LightSkyBlue"),
			         		new KeyValuePair<string, string>("#ff778899", "LightSlateGray"),
			         		new KeyValuePair<string, string>("#ffb0c4de", "LightSteelBlue"),
			         		new KeyValuePair<string, string>("#ffffffe0", "LightYellow"),
			         		new KeyValuePair<string, string>("#ff00ff00", "Lime"),
			         		new KeyValuePair<string, string>("#ff32cd32", "LimeGreen"),
			         		new KeyValuePair<string, string>("#fffaf0e6", "Linen"),
			         		new KeyValuePair<string, string>("#ffff00ff", "Magenta"),
			         		new KeyValuePair<string, string>("#ff800000", "Maroon"),
			         		new KeyValuePair<string, string>("#ff66cdaa", "MediumAquamarine"),
			         		new KeyValuePair<string, string>("#ff0000cd", "MediumBlue"),
			         		new KeyValuePair<string, string>("#ffba55d3", "MediumOrchid"),
			         		new KeyValuePair<string, string>("#ff9370db", "MediumPurple"),
			         		new KeyValuePair<string, string>("#ff3cb371", "MediumSeaGreen"),
			         		new KeyValuePair<string, string>("#ff7b68ee", "MediumSlateBlue"),
			         		new KeyValuePair<string, string>("#ff00fa9a", "MediumSpringGreen"),
			         		new KeyValuePair<string, string>("#ff48d1cc", "MediumTurquoise"),
			         		new KeyValuePair<string, string>("#ffc71585", "MediumVioletRed"),
			         		new KeyValuePair<string, string>("#ff191970", "MidnightBlue"),
			         		new KeyValuePair<string, string>("#fff5fffa", "MintCream"),
			         		new KeyValuePair<string, string>("#ffffe4e1", "MistyRose"),
			         		new KeyValuePair<string, string>("#ffffe4b5", "Moccasin"),
			         		new KeyValuePair<string, string>("#ffffdead", "NavajoWhite"),
			         		new KeyValuePair<string, string>("#ff000080", "Navy"),
			         		new KeyValuePair<string, string>("#fffdf5e6", "OldLace"),
			         		new KeyValuePair<string, string>("#ff808000", "Olive"),
			         		new KeyValuePair<string, string>("#ff6b8e23", "OliveDrab"),
			         		new KeyValuePair<string, string>("#ffffa500", "Orange"),
			         		new KeyValuePair<string, string>("#ffff4500", "OrangeRed"),
			         		new KeyValuePair<string, string>("#ffda70d6", "Orchid"),
			         		new KeyValuePair<string, string>("#ffeee8aa", "PaleGoldenrod"),
			         		new KeyValuePair<string, string>("#ff98fb98", "PaleGreen"),
			         		new KeyValuePair<string, string>("#ffafeeee", "PaleTurquoise"),
			         		new KeyValuePair<string, string>("#ffdb7093", "PaleVioletRed"),
			         		new KeyValuePair<string, string>("#ffffefd5", "PapayaWhip"),
			         		new KeyValuePair<string, string>("#ffffdab9", "PeachPuff"),
			         		new KeyValuePair<string, string>("#ffcd853f", "Peru"),
			         		new KeyValuePair<string, string>("#ffffc0cb", "Pink"),
			         		new KeyValuePair<string, string>("#ffdda0dd", "Plum"),
			         		new KeyValuePair<string, string>("#ffb0e0e6", "PowderBlue"),
			         		new KeyValuePair<string, string>("#ff800080", "Purple"),
			         		new KeyValuePair<string, string>("#ffff0000", "Red"),
			         		new KeyValuePair<string, string>("#ffbc8f8f", "RosyBrown"),
			         		new KeyValuePair<string, string>("#ff4169e1", "RoyalBlue"),
			         		new KeyValuePair<string, string>("#ff8b4513", "SaddleBrown"),
			         		new KeyValuePair<string, string>("#fffa8072", "Salmon"),
			         		new KeyValuePair<string, string>("#fff4a460", "SandyBrown"),
			         		new KeyValuePair<string, string>("#ff2e8b57", "SeaGreen"),
			         		new KeyValuePair<string, string>("#fffff5ee", "SeaShell"),
			         		new KeyValuePair<string, string>("#ffa0522d", "Sienna"),
			         		new KeyValuePair<string, string>("#ffc0c0c0", "Silver"),
			         		new KeyValuePair<string, string>("#ff87ceeb", "SkyBlue"),
			         		new KeyValuePair<string, string>("#ff6a5acd", "SlateBlue"),
			         		new KeyValuePair<string, string>("#ff708090", "SlateGray"),
			         		new KeyValuePair<string, string>("#fffffafa", "Snow"),
			         		new KeyValuePair<string, string>("#ff00ff7f", "SpringGreen"),
			         		new KeyValuePair<string, string>("#ff4682b4", "SteelBlue"),
			         		new KeyValuePair<string, string>("#ffd2b48c", "Tan"),
			         		new KeyValuePair<string, string>("#ff008080", "Teal"),
			         		new KeyValuePair<string, string>("#ffd8bfd8", "Thistle"),
			         		new KeyValuePair<string, string>("#ffff6347", "Tomato"),
			         		new KeyValuePair<string, string>("#ffffff", "Transparent"),
			         		new KeyValuePair<string, string>("#ff40e0d0", "Turquoise"),
			         		new KeyValuePair<string, string>("#ffee82ee", "Violet"),
			         		new KeyValuePair<string, string>("#fff5deb3", "Wheat"),
			         		new KeyValuePair<string, string>("#ffffffff", "White"),
			         		new KeyValuePair<string, string>("#fff5f5f5", "WhiteSmoke"),
			         		new KeyValuePair<string, string>("#ffffff00", "Yellow"),
			         		new KeyValuePair<string, string>("#ff9acd3", "YellowGreen")
			         	};
		}

		public static Color FromString(string colorString)
		{
			if (string.IsNullOrEmpty(colorString))
				return Colors.Transparent;

			if (colorString.StartsWith("#"))
				return FromHexCode(colorString);

			return FromName(colorString);
		}

		private static Color FromHexCode(string hex)
		{
			string h = hex.StartsWith("#") ? hex.Substring(1) : hex;

			byte a = System.Convert.ToByte(h.Substring(0, 2), 16);
			byte r = System.Convert.ToByte(h.Substring(2, 2), 16);
			byte g = System.Convert.ToByte(h.Substring(4, 2), 16);
			byte b = System.Convert.ToByte(h.Substring(6, 2), 16);
			return Color.FromArgb(a, r, g, b);
		}

		private static Color FromName(string name)
		{
			var colorValue =
				Lookup.FirstOrDefault(
					pair =>
					string.Compare(pair.Value, name.Trim(), CultureInfo.InvariantCulture,
					               CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols) == 0);

			return !string.IsNullOrEmpty(colorValue.Value) ? FromHexCode(colorValue.Key) : FromHexCode("#ff000000");
		}
	}
}